<template>
  <el-form
    status-icon
    ref="userInfoForm"
    :rules="userInforules"
    :model="userInfoForm"
    label-width="100px"
  >
    <el-form-item
      prop="password"
      label="密码"
      style="width:300px;"
    >
      <el-input
        type="password"
        v-model="userInfoForm.password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item
      prop="email"
      label="邮箱"
      style="width:300px;"
    >
      <el-input
        v-model="userInfoForm.email"
        placeholder="请输入邮箱地址"
        @keyup.enter.native="submitForm('userInfoForm')"
      ></el-input>
    </el-form-item>
    <div style="margin-left:auto;margin-right:auto;width:20%;">
      <el-button
        type="primary"
        @click="submitForm('userInfoForm')"
      >提交</el-button>
      <el-button @click="resetForm('userInfoForm')">重置</el-button>
    </div>
  </el-form>
</template>

<script>
export default {
  data () {
    return {
      userInfoForm: {
        username: null,
        email: null,
        password: null,
        newPassword: null,
        checkNewPassword: null
      },
      userInforules: {
        email: [
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          this.$http.put("/api/user",this.userInfoForm)
            .then((response) => {
              if(response.status == 200) {
                this.$message({
                  message: "邮箱更改成功",
                  type: "success",
                  duration: 3000
                });
              }
              else {
                this.$message({
                  message: "邮箱更改失败",
                  type: "error",
                  duration: 3000
                });
              }
              this.$refs.userInfoForm.resetFields()
            })
            .catch(function (error) {
              if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
              } else {
                // Something happened in setting up the request that triggered an Error
                console.log('Error', error.message);
              }
              console.log(error.config);
            })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
